package com.hl.util;

import java.util.HashMap;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * @category small date save
 * @author YangWenlong
 * @describe Cache Date
 * @date 2014-02-28
 * 
 */
public class SharedPerferenceUtils {
	/**
	 * sharedPerference save BaseInfo Save string[] of values
	 * 
	 * @param context
	 * @param key
	 * @param String
	 *            []
	 * @Method sharedPerferenceSave
	 */
	public static void sharedPerferenceStringGroupSave(Context context,
			String xmlText, String[] key, String[] value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		if (key != null && value != null) {
			for (int i = 0; i < key.length; i++) {
				editor.putString(key[i], value[i]);
			}
			editor.commit();
		} 
	}

	/**
	 * Save String of a value
	 * 
	 * @param context
	 * @param xmlText
	 * @param key
	 * @param String
	 */
	public static void sharedPerferenceStringSave(Context context, String xmlText,
			String key, String value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		if (key != null && value != null) {
			editor.putString(key, value);
			editor.commit();
		} 
	}

	/**
	 * Save boolean of a state value
	 * 
	 * @param context
	 * @param xmlText
	 * @param key
	 * @param boolean
	 */
	public static void sharedPerferenceBooleanSave(Context context, String xmlText,
			String key, Boolean value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		if (key != null && value != null) {
			editor.putBoolean(key, value);
			editor.commit();
		}
	}

	/**
	 * Save integer of a value
	 * 
	 * @param context
	 * @param xmlText
	 * @param key
	 * @param Integer
	 */
	public static void sharedPerferenceIntegerSave(Context context, String xmlText,
			String key, int value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		if (key != null) {
			editor.putInt(key, value);
			editor.commit();
		} 
	}

	/**
	 * sharedPerference read BaseInfo Read String[] of values
	 * 
	 * @param context
	 * @param key
	 * @param xmlText
	 * @Method sharedPerferenceRead
	 */
	public static HashMap<String, String> sharedPerferenceStringGroupRead(
			Context context, String xmlText, String[] key) {
		HashMap<String, String> map = new HashMap<String, String>();
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);

		for (int i = 0; i < key.length; i++) {
			if (key[i] != null) {
				map.put(key[i], sharedPreferences.getString(key[i], ""));
			}
		}
		return map;
	}

	/**
	 * Read string of a value
	 * 
	 * @param context
	 * @param xmlText
	 * @param key
	 * @return String
	 */
	public static String sharedPerferenceStringRead(Context context,
			String xmlText, String key) {
		String value = "";
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		if (key != null) {
			value = sharedPreferences.getString(key, "");
		}
		return value;
	}

	/**
	 * Read a state true or false
	 * 
	 * @param context
	 * @param xmlText
	 * @param key
	 * @return true or false
	 */
	public static boolean sharedPerferenceBooleanRead(Context context,
			String xmlText, String key) {
		boolean flag = false;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		if (key != null) {
			flag = sharedPreferences.getBoolean(key, false);
		}
		return flag;
	}

	/**
	 * Read integer of a integer
	 * 
	 * @param context
	 * @param xmlText
	 * @param key
	 * @return Integer
	 */
	public static int sharedPerferenceIntegerRead(Context context,
			String xmlText, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				xmlText, Context.MODE_PRIVATE);
		int value = 0;
		if (key != null) {
			value = sharedPreferences.getInt(key, 0);
		}
		return value;
	}
}
